package cate.game.play.config.buff;

import cate.common.table.fight.buff.row.BuffRow;
import cate.common.util.XT;
import cate.game.play.buff.Buff;
import cate.game.play.buff.BuffEnhanceCfg;
import cate.game.play.buff.BuffFactory;
import cate.game.play.buff.BuffParam;
import cate.game.play.fighter.Fighter;
import cate.game.play.support.PlayBuildContext;
import cate.game.table.TableManager;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuffCfgHolder {

    @NoteField("buff实例工厂")
    private BuffFactory factory = new BuffFactory();

    private Map<Integer, BuffCfg> map = new HashMap<>();

    public BuffCfg get(int id) {
        return map.get(id);
    }

    public void init(TableManager table) {
        map.clear();
        table.buff.base.getList().forEach(this::create);
    }

    public void init(PlayBuildContext context) {
        map.clear();
        context.buffExcel().base.getList().forEach(this::create);
    }

    private void create(BuffRow row){
        BuffCfg cfg = new BuffCfg(row);
        map.put(cfg.id, cfg);
    }


    public Buff build(Fighter owner, Fighter caster, int buffId, int roundAdd) {
        return build(owner, caster, buffId, roundAdd, null);
    }

    public Buff build(Fighter owner, Fighter caster, int buffId, int roundAdd, BuffParam param) {
        BuffCfg cfg = this.get(buffId);
        if (cfg == null) {
            return null;
        }
        List<BuffEnhanceCfg> enhanceList = owner.buff.getEnhance(cfg.row);
        if (XT.isNotBlank(enhanceList) || param != null) {
            cfg = new BuffCfg(cfg.row);
            for (BuffEnhanceCfg enhanceRow : enhanceList) {
                cfg.handleEnhanceCfg(enhanceRow);
            }
            if (param != null) {
                cfg.handleParam(param);
            }
        }
        return factory.build(owner, caster, cfg, roundAdd);
    }
}
